package com.pearadmin.modules.sys.controller.app;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.modules.sys.domain.RepInfo;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.modules.sys.service.IRepInfoService;

/**
 * 报表信息Controller
 *
 * @author Mr.Ling
 * @date 2022-01-04
 */
@RestController
@RequestMapping("/report/info")
public class RepInfoController extends BaseController {

    private String prefix = "report/info";

    @Autowired
    private IRepInfoService repInfoService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/report/info/main','report:info:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询报表信息列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/report/info/data','report:info:data')")
    public ResultTable list(@ModelAttribute RepInfo repInfo, PageDomain pageDomain) {
        PageInfo<RepInfo> pageInfo = repInfoService.selectRepInfoPage(repInfo, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增报表信息
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/report/info/add','report:info:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存报表信息
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/report/info/add','report:info:add')")
    public Result save(@RequestBody RepInfo repInfo) {
        repInfo.setId(SequenceUtil.makeId());
        return decide(repInfoService.insertRepInfo(repInfo));
    }

    /**
     * 修改报表信息
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/report/info/edit','report:info:edit')")
    public ModelAndView edit(Long id, ModelMap mmap) {
        RepInfo repInfo = repInfoService.selectRepInfoById(id);
        mmap.put("repInfo", repInfo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存报表信息
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/report/info/edit','report:info:edit')")
    public Result update(@RequestBody RepInfo repInfo) {
        return decide(repInfoService.updateRepInfo(repInfo));
    }

    /**
     * 删除报表信息
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/report/info/remove','report:info:remove')")
    public Result batchRemove(String ids) {
        return decide(repInfoService.deleteRepInfoByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/report/info/remove','report:info:remove')")
    public Result remove(@PathVariable("id") Long id) {
        return decide(repInfoService.deleteRepInfoById(id));
    }
}
